package com.zfsmart.component.common.util;

import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import lombok.Data;
import org.apache.commons.lang3.time.DateUtils;

import java.util.Date;
import java.util.Map;

/**
 * @Description token解密工具
 * @Author 薛铁琪
 * @CreateTime 2019-06-24 18:11
 * @Version 1.0
 */
@Data
public class TokenEncoder {
    private String key;

//    private boolean expired;

    private int expireTime;

    public String generateToken(Long accountId, Map<String, String> claims) {
        JwtBuilder builder = Jwts.builder()
                .setSubject(accountId.toString());
        //不适用token的有效期做校验 redis过期即无效 设置时间是为了token每次变化
        builder.setExpiration(DateUtils.addMinutes(new Date(), 1440));

        if (claims != null) {
            builder.claim("extends", claims);
        }

        return builder.signWith(SignatureAlgorithm.HS512, key)
                .compact();
    }
}
